package com.example.checkin.search;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.example.checkin.customview.GridViewWithHeaderAndFooter;
import com.example.checkin.vn.R;
import com.example.checkin.vn.adapter.Item;
import com.example.checkin.vn.adapter.SearchAdapter;

public class FragmentSearch extends Fragment {
	private static FragmentSearch fragment = null;

	public static FragmentSearch getInstance() {
		if (fragment == null) {
			fragment = new FragmentSearch();
		}
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.fragment_search, container,
				false);
		final GridViewWithHeaderAndFooter gridView = (GridViewWithHeaderAndFooter) view
				.findViewById(R.id.gridView1);
		final ProgressBar loading = (ProgressBar) view
				.findViewById(R.id.loading);
		new CountDownTimer(1000 * 1, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {

			}

			@Override
			public void onFinish() {
				loading.setVisibility(View.GONE);
				LayoutInflater layoutInflater = LayoutInflater
						.from(getActivity());
				View headerView = layoutInflater.inflate(
						R.layout.header_search, null);
				View footerView = layoutInflater.inflate(
						R.layout.footer_search, null);
				gridView.addHeaderView(headerView);
				gridView.addFooterView(footerView);

				List<Item> list = new ArrayList<Item>();
				for (int i = 0; i < 10; i++) {
					Item item = new Item();
					list.add(item);
				}
				SearchAdapter search = new SearchAdapter(getActivity(),
						R.layout.item_img_search, list);
				gridView.setAdapter(search);

			}
		}.start();

		return view;
	}
}
